package com.ymt.bpm.web.action.platform.profile;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Johnny on 2017/2/1.
 */
@Controller
public class TenantController {

    @Autowired
    private Environment env;

    /**
     * Must login to change tenant info
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/view/platform/profile/tenant/{id}", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request,
                              @PathVariable("id")String id) {
        String[] profile = env.getActiveProfiles();
        String productionVersion = env.getProperty("bpmp.production-version", "0");
        String cdn = env.getProperty("bpmp.cdn", "/bpmp-resources");
        ModelAndView view = new ModelAndView("layout/profile/tenant_edit");
        view.addObject("profile", profile[0]);
        view.addObject("productionVersion", productionVersion);
        view.addObject("cdn", cdn);
        view.addObject("tenantId", id);
        return view;
    }

    /**
     * Register page must be NOT secured
     * @param request
     * @return
     */
    @RequestMapping(value = "/pages/platform/profile/tenant/register", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request) {
        String[] profile = env.getActiveProfiles();
        String productionVersion = env.getProperty("bpmp.production-version", "0");
        String cdn = env.getProperty("bpmp.cdn", "/bpmp-resources");
        ModelAndView view = new ModelAndView("layout/profile/tenant_register");
        view.addObject("profile", profile[0]);
        view.addObject("productionVersion", productionVersion);
        view.addObject("cdn", cdn);
        view.addObject("appl", request.getParameter("appl"));
        return view;
    }

    /**
     * Register page must be NOT secured
     * @param request
     * @return
     */
    @RequestMapping(value = "/pages/platform/profile/tenant/term", method = RequestMethod.GET)
    public ModelAndView term(HttpServletRequest request) {
        String[] profile = env.getActiveProfiles();
        String productionVersion = env.getProperty("bpmp.production-version", "0");
        String cdn = env.getProperty("bpmp.cdn", "/bpmp-resources");
        ModelAndView view = new ModelAndView("help/term");
        view.addObject("profile", profile[0]);
        view.addObject("productionVersion", productionVersion);
        view.addObject("cdn", cdn);
        return view;
    }

}
